package flc.ast.adapter;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.calculator.ci.model.CiEachDetail;
import com.stark.calculator.ci.model.CiMethod;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.utils.ResUtil;

public class MyCiDetailAdapter extends StkProviderMultiAdapter<CiEachDetail> {

    private CiMethod mCiMethod;

    public MyCiDetailAdapter(CiMethod ciMethod) {
        super(1);
        this.mCiMethod = ciMethod;
        addItemProvider(new ItemProvider());
    }

    private class ItemProvider extends BaseItemProvider<CiEachDetail> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_my_ci_detail;
        }

        @Override
        public void convert(@NonNull BaseViewHolder holder, CiEachDetail ciEachDetail) {
            TextView tvNum = holder.getView(R.id.tvNum);
            int resId = R.string.cal_ci_year_fmt;
            if (ciEachDetail.ciMethod == CiMethod.MONTH) {
                resId = R.string.cal_ci_month_fmt;
            } else if (ciEachDetail.ciMethod == CiMethod.DAY) {
                resId = R.string.cal_ci_day_fmt;
            }
            tvNum.setText(getContext().getString(resId, ciEachDetail.num));

            holder.setText(R.id.tvPrincipal, String.format("%.2f", ciEachDetail.amount));
            holder.setText(R.id.tvRate, String.format("%.2f", ciEachDetail.rateAmount));
            holder.setText(R.id.tvSumRate, String.format("%.2f", ciEachDetail.sumRateAmount));
            holder.setText(R.id.tvPrincipalAndRate, String.format("%.2f", ciEachDetail.getOriAmountAndSumRate()));

            int pos = holder.getAdapterPosition();
            boolean showDesc = false;
            CiEachDetail preDetail = null;
            if (ciEachDetail.ciMethod != mCiMethod) {
                if (pos == 0) {
                    showDesc = true;
                } else {
                    preDetail = getItem(pos - 1);
                    if (ciEachDetail.ciMethod != preDetail.ciMethod) {
                        showDesc = true;
                    }
                }
            }
            TextView tvDesc = holder.getView(R.id.tvDesc);
            tvDesc.setVisibility(showDesc ? View.VISIBLE : View.GONE);
            if (showDesc) {
                StringBuilder builder = new StringBuilder();
                builder.append(mCiMethod.getName());
                builder.append(pos);
                builder.append(ResUtil.getStr(R.string.cal_ci_ci));
                builder.append("，");
                builder.append(ResUtil.getStr(R.string.cal_ci_syd) + (getRealDataCount() - pos));
                if (ciEachDetail.ciMethod == CiMethod.MONTH) {
                    builder.append(ResUtil.getStr(R.string.cal_ci_month));
                } else if (ciEachDetail.ciMethod == CiMethod.DAY) {
                    builder.append(ResUtil.getStr(R.string.cal_ci_day));
                }
                builder.append(ResUtil.getStr(R.string.cal_ci_bzy));
                if (mCiMethod == CiMethod.YEAR) {
                    builder.append(ResUtil.getStr(R.string.cal_ci_year));
                } else if (mCiMethod == CiMethod.MONTH) {
                    builder.append(ResUtil.getStr(R.string.cal_ci_month));
                }
                builder.append("，");
                builder.append(ResUtil.getStr(R.string.cal_ci_adljsrx) + "：");
                tvDesc.setText(builder.toString());
            }
        }
    }

}
